﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using pwblog.Entities;

namespace pwblog.DataAccess
{
    public class CategoryDao
    {
        private ManageBlogDataContext dataContext;

        public CategoryDao()
        {
            dataContext = new ManageBlogDataContext();
        }
        public Category[] GetCategories()
        {
            
            return dataContext.Categories.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        internal void InsertCategory(Category category)
        {
            dataContext.Categories.InsertOnSubmit(category);
            dataContext.SubmitChanges();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="idCategory"></param>
        /// <returns></returns>
        internal Category GetCategoryById(int idCategory)
        {
            return dataContext.Categories.Single(item => item.Id == idCategory);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="category"></param>
        internal void DeleteCategory(Category category)
        {
            dataContext.Categories.DeleteOnSubmit(category);
            dataContext.SubmitChanges();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="oldCategory"></param>
        internal void UpdateCategory(Category oldCategory)
        {
            var newCategory = dataContext.Categories.Single(item => item.Id == oldCategory.Id);
            newCategory.Name = oldCategory.Name;
            dataContext.SubmitChanges();
        }
    }
}